package mongo.common.dto;

import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * 返回实体类
 *
 * @param <T>
 */
public class ResponseBody<T> implements Serializable {

    private static final long serialVersionUID = 115765418882882264L;
    private Integer code;
    private String msg;
    private T data;

    public ResponseBody() {
        super();
    }

    public ResponseBody(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResponseBody<T> success(String msg, T data) {
        return new ResponseBody(ResponseCode.OK.CODE, StringUtils.isEmpty(msg) ? ResponseCode.OK.MSG : msg, data);
    }

    public static ResponseBody success(String msg) {
        return new ResponseBody(ResponseCode.OK.CODE, StringUtils.isEmpty(msg) ? ResponseCode.OK.MSG : msg, null);
    }

    public static <T> ResponseBody<T> error(String msg, T data) {
        return new ResponseBody(ResponseCode.ERR_BAD.CODE, StringUtils.isEmpty(msg) ? ResponseCode.ERR_BAD.MSG : msg, data);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResponseBody{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}